
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QWidget>
#include <QtSerialPort>


QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QWidget

{
    Q_OBJECT
signals:
    void ackOK();
public:
    /*发送数据包标志位*/
    enum DownloadState
    {
        DOWNLOAD_INVALID,       //无效状态
        DOWNLOAD_START,         //发送开始数据帧
        DOWNLOAD_START_WAIT_ACK,//等待开始回复帧
        DOWNLOAD_START_ACK,     //已等到开始回复帧
        DOWNLOAD_DATA_WAIT_ACK, //已发送数据，等待确认帧
        DOWNLOAD_DATA_ACK,      //已发送数据，已收到确认帧
        DOWNLOAD_FINISH,        //下载完成
    };
    Q_ENUM(DownloadState)
    DownloadState downloadState;//= QMetaEnum::fromType<SendState>();
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    void init();

private slots:
    void on_clearSendMsgBtn_released();

    void on_clearAckBtn_released();

    void on_openPortBtn_released();

    void on_startDownloadBtn_released();

    void on_openFileBtn_released();
    void onReadyReadCom();
    void sendOnceData();
    void send_end_signal();
    void sendDataFinish();
private:
    Ui::MainWindow *ui;
    QSerialPort mySerialPort;   //串口
    QByteArray binDataArray;    //读取到的bin文件数据
    int binDataLen = 0;         //bin文件大小

    int undownloadByte;         //未下载数据的大小
    int downloadedByte;         //已下载字节数
    int dataOnceLen;            //一次发送数据的长度
    int curDownloadDataLen = 0; //当前发送数据包的长度
    int sendErrTimes = 0;       //发送数据错误次数
    void send_start_signal();
};

#endif // MAINWINDOW_H
